<?php
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$mh_list_ids = 'LNPAN_MH_COPY_LIST_IDS';
$mh_list_images = 'MH_ZCOPY_LIST_IMAGES';

$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||mh_zcopy_list');

$limit = 50;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    die('stop');
  }
  $log_i  = 0;
  $timer  = time();
  $result = array();
  $where = "list_state=0 AND state in(0,1) ";
  $queue_ids = $redis->hKeys($mh_list_ids);
  //$queue_ids = clearLongLockID($redis, $mh_list_ids, $queue_ids);
  if($queue_ids) {
    $where .= 'AND id not in('.implode(',', $queue_ids).') ';
  }
  //前10个里随机取一个
  $rand  = rand(0,10);
  $limit = "$rand, 1";
  //$where = 'id=125 '; $limit = '0,1 ';
  
  $info = $model->find('news_zcopy', $where, 'news_state desc', $limit);
  $model->close();
  if($info) {
    $redis->hSet($mh_list_ids, $info['id'], date('Y-m-d H:i:s').'@@ '.$info['title'].$info['link']);

    $urls_enough = false;
    $urls = array();
    $chapters = json_decode($info['chapter'], true);
    //0.start
    log_timer($redis, $timer, $info, '00.find_end: ', $log_i);
    $timer  = time();
    foreach ($chapters as $title => $list) {
      if($urls_enough) break;
      foreach ($list as $url => $tit) {
        $url = str_replace(' ', '%20', $url);
        $url_key = getMD5($url);
        //$old = $model->find('news_images', "url_key='$url_key'");
        $old = $redis->hGet($mh_list_images, $url_key);
        if(empty($old)) {
          $urls[] = $url;
          if(count($urls) >= 10) {
            $urls_enough = true;
            break;
          }
        }
      }
    }
    $result['id'] = $info['id'];
    //$result['urls'] = $urls;

    if(count($urls) > 0) {
      $link = $info['link'];
      $parse_link = parse_url($link);
      $opt_data = array(
        CURLOPT_REFERER => $link,
        CURLOPT_ENCODING=> 'gzip',
        CURLOPT_HTTPHEADER => array(
          'Referer' => $link,
          'Host' => $parse_link['host'],
          'Pragma' => 'no-cache',
        ),
      );
      $http->opt = $opt_data;
      //1.check_end
      log_timer($redis, $timer, $info, '01.check_end: ', $log_i);
      $timer  = time();
      $responses = $http->curl_http($urls);

      //2.http_end
      log_timer($redis, $timer, $info, '02.http_end: ', $log_i);
      $timer  = time();
      foreach ($responses as $url => $item) {
        $data = $cut->getImagesUrl_ByFile($info, $item);
        if(is_array($data) && count($data) > 0) {
          $new_data = array(
            'url' => $url,
            'news_id' => $info['id'],
            'url_key' => getMD5($url),
            'images1' => json_encode($data),
            'images1_count' => count($data),
          );
          $ret = $model->add('news_images', $new_data);
          $mysql_error = mysql_error();
          if($ret) {
            $news_images_id = $model->insert_id();
            $redis->hSet($mh_list_images, $new_data['url_key'], $info['id'].':'.$news_images_id.':'.$new_data['url']);
          } elseif(stristr($mysql_error, 'Duplicate')) {
            $result['exists'][] = $info['id'].':'.$new_data['url'];
            $redis->hSet($mh_list_images, $new_data['url_key'], $info['id'].':'.$mysql_error);
          }

          //$new_data['images1'] = count($data);
          //$result['images1'][] = $new_data;
        } else {
          $result[$url] = $item;
          if($item['info']['http_code'] == 404) {
            //$updata = array('list_state'=>2, 'news_state'=>0);
            $updata = array('uptime'=>time());
            $model->save("news_zcopy", "id='$info[id]'", $updata);
          }
        }
      }
      $counter = $model->query("select count(*) as count from news_images where news_id='$info[id]'");
      $result['progress'] = $counter[0]['count'].'/'.$info['chapter_count'];

      //3.insert_end
      $extra = ', '.$result['progress'];
      log_timer($redis, $timer, $info, '03.insert_end: ', $log_i, $extra);
      $timer  = time();
    }
    else {
      $counter = $model->query("select count(*) as count from news_images where news_id='$info[id]'");
      if($counter[0]['count'] == $info['chapter_count']) {
        $left_chapter = substr($info['chapter'], 0, 4);
        $news_state = $left_chapter == '{"\u' ? 1 : 9;
        $updata = array('list_state'=>1, 'news_state'=>$news_state);
        $model->save("news_zcopy", "id='$info[id]'", $updata);
        $result['success'] = $info;
      }

      //$redis->hSet('LNPAN_MH_COPY_LIST', $info['id'], date('Y-m-d H:i').$info['title'].':'.$info['link']);
    }

    $redis->hDel($mh_list_ids, $info['id']);
    $model->close();
  }
  else {
    $result['msg'] = 'finished';
    sleep(1);
  }

  //print_r($result);
  //if($result['progress'] || $result['success'])
    //$redis->lPush('MH_ZCOPY_LIST_LOG', print_r($result, true));
  //exit;
}